//
//  statusListViewModel.swift
//  Weibo
//
//  Created by 明丽 on 16/1/22.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import AFNetworking

class statusListViewModel: NSObject {

    //定义模型数组
    var statuses = [Status]()
    func loadHomeData(ispilling: Bool,finished: (isSuccess: Bool)->()){
    
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        guard let token = UserAccountViewController().token else {
           
            print("用户暂未登录")
            finished(isSuccess: false)
            return
            
        }
        var paramerters = ["access_token": token]
        //用系统的会把已经有的数据重新加载,只要帅新都会加载,通过端口我们可以获取数据的ID,刷新的ID是数组的第一个元素
        //可选项,一旦为空位置为0
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
        // let since_id = self.statues.first?.id ?? 0
        if ispilling {
            //上拉加载数据
            max_id = self.statuses.last?.id ?? 0
            
        }else {
            //下拉刷新数据
            since_id = self.statuses.first?.id ?? 0
            
        }
        
        if since_id > 0 {
            paramerters["since_id"]="\(since_id)"
            
        }
        if max_id > 0 {
            
            paramerters["max_id"] = "\(max_id)"
        }
        
        
        let AFN = AFHTTPSessionManager()
        AFN.GET(urlString, parameters: paramerters, progress: nil , success: { (_, result) -> Void in
            //print("result")
            //结束刷新
            //self.refreshControl?.endRefreshing()
            
            guard let dict = result as? [String : AnyObject] else {
                
                print("数据结构错误")
               // SVProgressHUD.showErrorWithStatus(AppErrorTip)
                
                finished(isSuccess: false)
                return
            }
            guard let array = dict["statuses"] as? [[String : AnyObject]] else{
                print("数据结构错误")
                //SVProgressHUD.showErrorWithStatus(AppErrorTip)
                finished(isSuccess: false)
                return
            }
            //定义一个数组容器
            var tempArray = [Status]()
            
            for item in array {
                //print(item)
                let s = Status(dict: item)
                //添加元素
                tempArray.append(s)
            }
            //根据since_id来判断是否是下拉刷新
            if since_id > 0 {
                self.statuses = tempArray + self.statuses
            }else if max_id > 0 {
                
                
                self.statuses += tempArray
                //上拉结束完之后刷新动画
                //self.indicatorView.stopAnimating()
                
            }else {
                //首次加载数据
                self.statuses = tempArray
                
                
            }
            //测试刷新到多少数据
//            print("刷新了",self.statuses.count)
//            
//            self.tableView.reloadData()
            finished(isSuccess: true)
            }) { (_, error) -> Void in
                //SVProgressHUD.showErrorWithStatus(AppErrorTip)
                
                print("error")
        }

}
}